package GaoDe;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class CodeSearch {

    public static void main(String[] args) throws IOException {
        //读取Downloads文件夹重点gaode.xlsx
        FileInputStream in = new FileInputStream(new File("/Users/mac/Downloads/gaode.xlsx"));

        Workbook workbook = new XSSFWorkbook(in);

        Sheet sheet = workbook.getSheetAt(0);

        int rowNum = sheet.getLastRowNum();

        //创建一个hashmap存储city对象，它有两个属性，邮编和城市名

        Map <String, City> map = new HashMap <String, City>();

        for (int i = 2; i < rowNum; i++) {
            City city = new City();
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String abcode = row.getCell(1).getStringCellValue();
            if (row.getCell(2)!=null){
            String citycode = row.getCell(2).getStringCellValue();
                city.setCitycode1(citycode);
            }
            city.setName1(name);
            city.setCode1(abcode);


            System.out.println(citycode);
            map.put(city.getCode1(), city);

        }
        //io
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        //让io可以一直存在

        while (true) {
            System.out.println("请输入想查询城市的邮编");
            try {
                String code = bufferedReader.readLine();
                //输入的非表格中的邮编时，提示并回到io
                if (!map.containsKey(code)) {
                    System.out.println("未找到该城市《" + code + "》");
                } else {

                    City city1 = map.get(code);

                    String a = city1.getCode1();

                    //这个方法是分解字符串并切拼接省、市、直辖市的邮编（从excel中发现的编码规律）

                    char[] ch = a.toCharArray();

                    String b = String.valueOf(ch[0]) + ch[1];
                    String c = String.valueOf(ch[2]) + ch[3];
                    String x = String.valueOf(ch[4]) + ch[5];
                    String d = "01";
                    String e = "00";
                    String f = b + e + e;
                    String g = b + c + e;
                    String j = "0";

                    //当邮编属于四个直辖市的分支
                    City didian2 = map.get(f);
                    City didian3 = map.get(g);
                    if (b.equals("12") || b.equals("11") || b.equals("31") || b.equals("50")) {

                        System.out.println("直辖市的名称是：" + didian2.getName1());
                        System.out.println("区的名称是：" + city1.getName1());
                        System.out.println("区的邮编是：" + city1.getCode1());
                        //邮编如果市直辖市本体
                        if (c.equals(e)) {

                            System.out.println("它辖下的区有：");
                            //这段代码的逻辑是遍历市辖下的区

                            for (int h = 0; h < 30; h++) {
                                String s = String.valueOf(h);
                                if (h < 10) {
                                    s = j + s;
                                }
                                String k = b + d + s;
                                City didian4 = map.get(k);

                                if (didian4 != null) {
                                    System.out.println("区的名称是：" + didian4.getName1());
                                    System.out.println("区的邮编是：" + didian4.getCode1());
                                    System.out.println("  ");
                                }
                            }
                        }
                        // 特别行政区
                    } else if (b.equals("81") || b.equals("82")) {
                        System.out.println("特别行政区的名称是：" + didian2.getName1());
                        System.out.println("区的名称是：" + city1.getName1());
                        System.out.println("区的邮编是：" + city1.getCode1());
                        if (c.equals("00")) {

                            System.out.println("它辖下的区有：");
                            for (int l = 1; l < 30; l++) {
                                String str = String.valueOf(l);
                                if (l < 10) {
                                    str = j + str;
                                }
                                String m = b + e + str;
                                City didian5 = map.get(m);
                                if (didian5 != null) {
                                    System.out.println("区的名称是：" + didian5.getName1());
                                    System.out.println("区的邮编是：" + didian5.getCode1());
                                    System.out.println(" ");
                                }
                            }
                        }
                    }
                    //省以及自治区
                    else {

                        System.out.println("省的名称是：" + didian2.getName1());
                        System.out.println("市的名称是：" + didian3.getName1());
                        System.out.println("区/县的名称是：" + city1.getName1());
                        System.out.println("区/县的邮编是：" + city1.getCode1());
                        if (c.equals("00")) {

                            System.out.println("它辖下的市有：");
                            for (int n = 1; n < 30; n++) {
                                String string = String.valueOf(n);
                                if (n < 10) {
                                    string = j + n;
                                }
                                String o = b + string + e;
                                City didian6 = map.get(o);
                                if (didian6 != null) {
                                    System.out.println("市的名称是：" + didian6.getName1());
                                    System.out.println("市的邮编是：" + didian6.getCode1());
                                    System.out.println("    ");
                                }
                            }
                        } else if (x.equals("00")) {
                            System.out.println("它辖下的区有：");
                            for (int y = 1; y < 30; y++) {
                                String str2 = String.valueOf(y);
                                if (y < 10) {
                                    str2 = j + str2;

                                }
                                String z = b + c + str2;
                                City didian7 = map.get(z);
                                if (didian7 != null) {
                                    System.out.println("区的名称是：" + didian7.getName1());
                                    System.out.println("区的邮编是：" + didian7.getCode1());
                                    System.out.println("");
                                }
                            }
                        }
                    }
                }


                System.out.println("----------------------");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}